package user.view;


import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import db.FlightsManage;
import javax.swing.JLabel;

public class UserHistoryFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -343774353414546019L;
	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel historyDataModel;
	private JLabel total_label;
	private static UserHistoryFrame inst;
	public static UserHistoryFrame getInstance()
	{
		if(inst==null)
		{
			inst=new UserHistoryFrame();
		}
		return inst;
	}
	

	public void updateMyHistory()
	{
		
		ArrayList<String[]> t = FlightsManage.getBookings(LoginView.getInstance().getUsername());
		historyDataModel.setNumRows(0);
		int col = 7;
		float s = 0;
		for (int j = 0; j < t.size(); j++) {

			Object[] os = new Object[col];
			for (int i = 0; i < col; i++)
				os[i] = ((String[]) t.get(j))[i];
			try {
				s += Float.parseFloat(os[4].toString());
			} catch (Exception e) {
				
			}

			historyDataModel.addRow(os);

		}
		total_label.setText("Total:  " + s + "($)");
	}
	/**
	 * Create the frame.
	 */
	public UserHistoryFrame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 687, 461);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 10, 651, 380);
			contentPane.add(scrollPane);
			{
				table = new HistoryTable();
				scrollPane.setViewportView(table);
				{
					total_label = new JLabel("");
					total_label.setBounds(349, 400, 312, 15);
					contentPane.add(total_label);
				}
				historyDataModel = new DefaultTableModel();
				historyDataModel.setColumnIdentifiers(new String[] { "Order Time","Line ID","Take off Date",
						"Take Off Time", "Price($)", "Departure","Destination" });
				table.setModel(historyDataModel);
				table.setRowHeight(25);
			}
		}
		updateMyHistory();
	}
	class HistoryTable extends JTable
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = 7479497893090435218L;

		public boolean isCellEditable(int row, int column) {
			
				return false;

			
		}
	}
}
